package com.orangelife.utils;

import java.math.BigDecimal;
import java.util.Stack;

import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeComputeRule.model.FormulaModal;

public class FormulaUtil {
	
	
	public static void main(String[] args) {
		
		String pp="333333";
		System.out.println(pp.replace("3", "4"));
		
		System.out.println(inTosuffix("(;(;9;+;(;3;-;1;);););*;3;+;10;/;3.0;+;(;1;+;2;)"));
		System.out.println(complete(inTosuffix("(;(;9;+;(;3;-;1;);););*;3;+;10;/;3.0;+;(;1;+;2;)")));
		System.out.println(complete(inTosuffix("(;(;9;+;(;3;-;1;);););*;3;+;10;/;3.0;+;(;1;+;2;);")));
		
		System.out.println( "ll;l;l".split(";").length);
		
		
	}
	/**
	 * 中缀表达式变成后缀表达式((;(;9;+;(;3;-;1;);););*;3;+;10;/;3.0;+;(;1;+;2)
	 * @param infix(待验证字符串)
	 * 
	 * @author
	 * @date 创建时间 2016-07-25 10:41:10
	 * 
	 * @return String
	 * @throws AppBizException
	 */
	
	public static String inTosuffix(String infix){
		
		if(!check(infix)){
			
			
			return null;
		}
		
		Stack<String> stack = new Stack<String>(); 
		  Stack<String> stack2 = new Stack<String>(); 
	
		  String mm[]=infix.split(";");
		  
		  int len=mm.length;
		  String m=null;

		  for(int i=0;i<len;i++){
			  m=mm[i];
			  
			if("(".equals(mm[i])){
		
				stack.push(mm[i]);
				
			}else if(")".equals(mm[i])){
				
			
				while(!stack.empty()){
					if("(".equals(stack.peek())){
						
						stack.pop();
						break;
						
					}else{
			        stack2.push(stack.pop());
						
						
					}
					
				}			
				
			}else{
				
				if("+".equals(m)||"-".equals(m)||"*".equals(m)||"/".equals(m)){
								
					while(true){
						
						if(stack.isEmpty()){
						
							stack.push(m);
							break;	
							
						}
						
						
						if("(".equals(stack.peek())){
						
					
							stack.push(m);
							break;
						}
						if(compareOp(m,stack.peek())){						
							stack.push(m);
							break;
							
						}else{
							
							stack2.push(stack.pop());
				
							
						}
						
						
					}			
					
					
				}else{
					stack2.push(m);
				
				}
				
				
			}
			  
			   
			  
		  }
		  
		  
		  while(!stack.empty()){
				stack2.push(stack.pop());		 
		  }
		  
		  
		  
		  
		  
		  
		  StringBuffer  kk=new StringBuffer();
		  for (String string : stack2) {
			  kk.append(string+";");
		

		}
		return kk.toString();
		
	}
	
	
	
	
	
	
	
	/**
	 * 公式校验kkk="(;(;9;+;(;3;-;1;);););*;3;+;10;/;3.0;+;(;1;+;2";
	 * @param FormulaString(待验证字符串)
	 * 
	 * @author
	 * @date 创建时间 2016-07-25 10:41:10
	 * 
	 * @return boolean
	 * @throws AppBizException
	 */

	public static boolean check(String FormulaString){
			
		  Stack<String> stack = new Stack<String>(); 
          String [] l=FormulaString.split(";");
		  String temp="";
		  String tp="";
		  for(int i=0;i<l.length;i++){
			  
			  
			  if(")".equals(l[i])){
				  temp="";	
				  tp="";
				  tp=stack.pop();			
				
				while(!"(".equals(tp)){
					
					  temp=tp+temp;
					  
					  if(stack.isEmpty()){
						  
						  return false;
					  }
					  tp=stack.pop();
				  }
					
				if(checkSubFormula(temp)){
					
				stack.push("s");	
					
					
				}	
				else{
					
					
					return false;
				}
		
				}else{
					
					if("/*+-()".indexOf(l[i])==-1){
						stack.push("s");
						
						
					}else{
						
						stack.push(l[i]);	
						
					}
					
					
					
				}
			  
			  
			  
		  }
		  temp="";
		  for(int j=0;j<stack.size();j++){
			  temp+= stack.get(j);	  
		  }
	
		if(!checkSubFormula(temp)){
			
			return false;
			
		};
		
		return true;
		
		
	}
	/**
	 * 验证除括号外的公式（只适合加减乘除/)这里其他参数使用s替代
	 * @param FormulaString(待验证字符串)
	 * * @param reString(参数替换字符串)
	 * @author
	 * @date 创建时间 2016-07-25 10:41:10
	 * 
	 * @return
	 * @throws AppBizException
	 */


	public   static boolean checkSubFormula(String FormulaString){
		
		FormulaString="+"+FormulaString;
		
	     return FormulaString.matches("^(([\\+\\/\\*\\-]s)+)$");
		
		

	}
	/**
	 * 验证处括号外的公式（只适合加减乘除/)这里其他参数使用s替代
	 * @param FormulaString(待验证字符串)
	 * * @param reString(参数替换字符串)
	 * @author
	 * @date 创建时间 2016-07-25 10:41:10
	 * 
	 * @return
	 * @throws AppBizException
	 */


	public   static boolean checkSubFormula(String FormulaString,String reString){
		
		FormulaString="+"+FormulaString;
		
	     return FormulaString.matches("^(([\\+\\/\\*\\-]"+reString+")+)$");
		
		

	}
	public static boolean compareOp(String a,String b){
		
		

		int n=1;
		int m=1;
		
		
		
		
		if("*/".indexOf(a)==-1){
		
			n=0;
		}

		if("*/".indexOf(b)==-1){
		
			m=0;
		}
		
		
		if(n>m){
			
			return true;
		}
		
		return false;
		
	}
	public static String completeString(String s){
		 Stack<String> stack3 = new Stack<String>(); 

		
		
		String[] stack2=s.split(";");
		 for (String string : stack2) {

			  if("+-*/".indexOf(string)==-1){
				  
				  stack3.push(string) ;
				  
			    }else{
				  
			    	BigDecimal a=	  new BigDecimal(stack3.pop().toString()) ;
					BigDecimal b=	 new BigDecimal(stack3.pop().toString()) ;
				  if("+".equals(string)){
					  
					  stack3.push(a.add(b).toString()); 
					  
				  }
				  else if("-".equals(string)){
					  
		
					  stack3.push(b.subtract(a).toString()); 

					  
				  }
	              else if("*".equals(string)){
			 		  
					  stack3.push(b.multiply(a).toString()); 

					  
				  }
	              else if("/".equals(string)){
	            	  
	            	  if(a.compareTo(BigDecimal.ZERO)==0)return null;
		  
					  stack3.push(b.divide(a,9,BigDecimal.ROUND_HALF_UP).toString()); 

		  
	              }
				
				  
				  
			  }

		}
if(stack3.size()==1){
	
	return stack3.pop();
	
}
		
		return null;
		
	}
	public static BigDecimal complete(String s){
		 Stack<String> stack3 = new Stack<String>(); 

		
		
		String[] stack2=s.split(";");
		 for (String string : stack2) {

			  if("+-*/".indexOf(string)==-1){
				  
				  stack3.push(string) ;
				  
			    }else{
				  
			    	BigDecimal a=	  new BigDecimal(stack3.pop().toString()) ;
					BigDecimal b=	 new BigDecimal(stack3.pop().toString()) ;
				  if("+".equals(string)){
					  
					  stack3.push(a.add(b).toString()); 
					  
				  }
				  else if("-".equals(string)){
					  
		
					  stack3.push(b.subtract(a).toString()); 

					  
				  }
	              else if("*".equals(string)){
			 		  
					  stack3.push(b.multiply(a).toString()); 

					  
				  }
	              else if("/".equals(string)){
	            	  
	            	  if(a.compareTo(BigDecimal.ZERO)==0)return null;
		  
					  stack3.push(b.divide(a,9,BigDecimal.ROUND_HALF_UP).toString()); 

		  
	              }
				
				  
				  
			  }

		}
   if(stack3.size()==1){
	
 	  return new BigDecimal(stack3.pop()) ;
	
    }
		
		return null;
		
	}
	
	/**
	 * 自定义公式解析器（不支持嵌套） 返回方法名 和参数值
	 * */
	
	public static  FormulaModal parseSelfFormulaSimple(String formula){
		FormulaModal f=	new FormulaModal(formula);		
		f.parseFormulaSimple();
		return f;
		
	}
	
}
